<?
$step_1_edit = translate('step_1_edit');
$step_2_set = translate('step_2_set');
$minprice_th = translate('minprice_th');
$maxprice_th = translate('maxprice_th');
$wholesale_th = translate('wholesale_th');
$price_th = translate('price_th');
$price_th_y = translate('price_th_y');

$cancel_button = translate('cancel');
$next_step = translate('next_step');
$clear_form = translate('clear_form');
$planname = translate('planname');
$planperiod = translate('planperiod');
$months = translate('months');
$tlservice = translate('tlservice');
$tlquantity = translate('tlquantity');
$tlmo = translate('tlmo');
$tlbaseunit = translate('tlbaseunit');
$tlunitprice = translate('tlunitprice');
$totmonthprice = translate('totmonthprice');
$save_plan = translate('save_plan');

echo "<div align=center>
<table border='0' cellspacing='0' cellpadding='5'>
	<tr>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td valign='middle' align='center'>
			<img src='images/ico_step_2.gif' border='0'>
		</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td valign='middle' align='center'>
			<img src='images/ico_completed_step.gif' border='0'>
		</td>
		<td valign='middle' align='center'>$step_1_edit</td>
		<td valign='middle' align='center'>
			<img src='images/ico_current_step.gif' border='0'>
		</td>
		<td valign='middle' align='center'><font color='#FF6000'>$step_2_set</font></td>
	</tr>
</table>
<script language='JavaScript'>
<!--
function calc_price() {
	var mopr = document.getElementById('new_price').value;
	mopr=parseFloat(mopr);
	document.getElementById('yprice_div').innerHTML = '$' + display_dec_string(mopr*12);
}

function display_dec_string(input) {
	var ost = 0;
	var cch = 0;
	var result;
	cch = Math.floor(input);
	if( cch == 0 ) {
		ost = input;
	} else {
		ost = input % cch;
	}
	ost = Math.round(ost * 100);
	if ( ost < 10 ) ost = \"0\" + ost;
	result = cch + \".\" + ost;
	return result;
}

function pafY() {
	var minpr = document.getElementById('minpr').value;
	var maxpr = document.getElementById('maxpr').value;
	var new_price = document.getElementById('new_price').value;

	minpr = parseFloat(minpr);
	maxpr = parseFloat(maxpr);
	new_price = parseFloat(new_price);
	if (new_price<minpr) {
		alert('".translate('below_minimal_price')."');
		document.form1.reset();
		calc_price();
		return false;
	}
	if (new_price>maxpr) {
		alert('".translate('above_maximal_price')."');
		document.form1.reset();
		calc_price();
		return false;
	} else {
		return true;
	}
}
//-->
</script>";
$step2_message = translate('step2_message',array('planname' => $plname));
echo "<br><div align='left'>$step2_message</div></br><br>";
if (empty($prod_id)) {
	$errorMsg = translate('int_error');
	include_once('errorMsg.html');
	exit();
}
switch ($case) {
	case 2:
		if (empty($_POST['plan_name'])) {
			$errorMsg = translate('noplanname');
			include_once('errorMsg.html');
			exit();
		} elseif (!preg_match("/^[a-zA-Z0-9\-]{1,10}$/", $_POST['plan_name'])  or in_array($_POST['plan_name'],array('Business','Corporate'))) {
			$errorMsg = translate('planname_doesnt_match');
			include_once('errorMsg.html');
			exit();
		} else {
			$plan_name=htmlentities($_POST['plan_name']);
			$plan_name=pg_escape_string($_POST['plan_name']);

        	        $query = "SELECT product_id, name
				FROM products
				WHERE name = '$plan_name'
				AND reseller_id='$id'
				AND product_id != '$prod_id'";
			$result = pg_query($dbh81,$query);
			$num = pg_num_rows($result);
			if ($num != 0) {
				$errorMsg = translate('plan_exists');
				include_once('errorMsg.html');
				exit();
			}
			//------------------------------------------------------
				$sql['type']='SELECT';
				$sql['params']="services";
				$sql['table']='products';
				$sql['where']="product_id = '$prod_id' AND reseller_id = '$id'";
				$sql['options']='';
				$sql['return']='struct';
			//------------------------------------------------------
			if (!$result=sql_exec($sql,$dbh81,'assoc')) {
				$errorMsg = translate('int_error');
				include_once('errorMsg.html');
				exit();
			}
			$services = $result[0]['services'];
			if ($_POST['site_studio'] == 1 or $_POST['site_studio'] == 0) {
				$services['site_studio'] = $_POST['site_studio'];
			} else {
				$errorMsg = translate('int_error');
				include_once('errorMsg.html');
				exit();
			}
			$services = base64_encode(serialize($services));
			$sql['type']    = "UPDATE";
			$sql['params']  = "name='$plan_name',services='$services'";
			$sql['table']   = "products";
			$sql['where']   = "reseller_id='$id' AND product_id='".$_POST['prod_id']."'";
			$sql['options'] = "";
			$sql['return']  = "";
			$result =  sql_exec($sql,$dbh81);
			if (!$result) {
				$errorMsg = translate('int_error');
				include_once('errorMsg.html');
				exit();
			}
		}
	case 1:
        	//------------------------------------------------------
        	        $sql['type']='SELECT';
        	        $sql['params']='name, min_price, price, max_price, wholesale';
        	        $sql['table']='products';
        	        $sql['where']="product_id='$prod_id'";
        	        $sql['options']='';
        	        $sql['return']='struct';
        	//------------------------------------------------------
        	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
        	        $errorMsg = translate('int_error');
        	        include_once('errorMsg.html');
        	        exit();
        	}
		$plan_name = $result[0]['name'];
		$minpr = sprintf("%01.2f",$result[0]['min_price'][12]['USD']/12);
		$maxpr = sprintf("%01.2f",$result[0]['max_price'][12]['USD']/12);
		$monpr = sprintf("%01.2f",$result[0]['price'][12]['USD']/12);
		$wholesale_mo = sprintf("%01.2f",$result[0]['wholesale'][12]['USD']/12);
		break;
	case 3:
		if (empty($_POST['plan_name'])) {
			$errorMsg = translate('noplanname');
			include_once('errorMsg.html');
			exit();
		} elseif (!preg_match("/^[a-zA-Z0-9\-]{1,10}$/", $_POST['plan_name'])   or in_array($_POST['plan_name'],array('Business','Corporate'))) {
			$errorMsg = translate('planname_doesnt_match');
			include_once('errorMsg.html');
			exit();
		} else {

			//------------------------------------------------------
				$sql['type']='SELECT';
				$sql['params']='currency, store_id';
				$sql['table']='stores';
				$sql['where']="reseller_id='$id'";
				$sql['options']='';
				$sql['return']='struct';
			//------------------------------------------------------
			if (!$result=sql_exec($sql,$dbh81,'assoc')) {
				$errorMsg = translate('int_error');
				include_once('errorMsg.html');
				exit();
			}
			$res_currency=$result[0]['currency'];
			$store_id=$result[0]['store_id'];

			//------------------------------------------------------
				$sql['type']='SELECT';
				$sql['params']="key_name, quantity, $res_currency as currency";
				$sql['table']='plan_elements AS a, plan_prices AS b';
				$sql['where']="a.id=b.id and a.key_name!='cgi'";
				$sql['options']='order by b.id,quantity';
				$sql['return']='struct';
			//------------------------------------------------------
			if (!$result=sql_exec($sql,$dbh81,'assoc')) {
				$errorMsg = translate('int_error');
				include_once('errorMsg.html');
				exit();
			}
			$plan_name=htmlentities($_POST['plan_name']);
			$plan_name=pg_escape_string($_POST['plan_name']);
			$period=$_POST['period'];
			foreach ($_POST as $key => $value) {
				if ($key!='plan_name' && $key!='period' && $key!='submit' && $key!='form_sent') {
					if (preg_match("/^key_.*/",$key)) {
						$key=preg_replace("/^(key_)/",'',$key);
						$newarr[$key]=$value;
					}
				}
			}
			foreach ($newarr as $key => $value) {
				if ($key=='disk_space') $value=$value*1024;
				if ($key=='traffic') $value=$value*1024000;
				$services[$key]=$value;
			}
			$tmp=$services['mailbox']*2;
			$services['aliases']=$tmp;
			$services['fpe']=9999;
			$services['ppa']=9999;
			#$services['site_studio']=0;
			$services['ssl']=9999;
			$services['filemanager']=1;
			$services['stats']=1;
			$services['redirect_url']=10;
			$services['backup']='-1';
			$services['webmail']=1;
			$services['phpmy_admin']=1;
			#$services['support']=1;
			$services['template']='-1';
			$services['smtp']=1;
			$services['register_domain']=1;
			#$tmp=$services[domain]*2;
			#$services[subdomain]=$tmp;
			$tmp=$services['sql']*30;
			$services['sql_quota']=$tmp;
			foreach ($result as $rslt) {
				if ($newarr[$rslt['key_name']]==$rslt['quantity'])
					$prices[$rslt['key_name']]=$rslt['currency'];
			}
			$monpr=sprintf("%01.2f",array_sum($prices));
			$minpr=sprintf("%01.2f",$monpr + ($monpr*5)/100);
			$maxpr=sprintf("%01.2f",$monpr + ($monpr*125/100));
			$wholesale_mo = $monpr;
			$control_hidden = 1;
		}
		break;
}
$params['sid'] = $sid;
$params['sub'] = 'edit';
$params['action'] = "managePlans";
$params['finish_steps'] = 'yes';
$actionurl = "$baseURL/$webDIR/?".encode_params($params);

//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='name,min_price,max_price,price,wholesale';
	$sql['table']='products';
	$sql['where']="product_id='$prod_id'";
	$sql['options']='';
	$sql['return']='struct';
//------------------------------------------------------
if (!$prres=sql_exec($sql,$dbh81,'assoc')) {
	$errorMsg = translate('int_error');
	include_once('errorMsg.html');
	exit();
}

$o_name = $prres[0]['name'];
$o_min_price = "\$".sprintf("%01.2f",$prres[0]['min_price'][12]['USD']/12);
$o_max_price = "\$".sprintf("%01.2f",$prres[0]['max_price'][12]['USD']/12);
$o_price = "\$".sprintf("%01.2f",$prres[0]['price'][12]['USD']/12);
$o_rp = "\$".sprintf("%01.2f",$prres[0]['price'][12]['USD']);
$o_wholesale = "\$".sprintf("%01.2f",$prres[0]['wholesale'][12]['USD']/12);
$curr_prices = translate('curr_prices');
$new_prices = translate('new_prices');
$old_pl_name = translate('old_pl_name');
echo "<div align='center'>
<table width='550' class='bTable' cellspacing=0>
	<tr>
		<th colspan='6' align='left'>$curr_prices</th>
	</tr>
	<tr>
		<th class='text' width='17%'>$old_pl_name</th>
		<th class='text' width='17%'>$minprice_th</th>
		<th class='text' width='17%'>$maxprice_th</th>
		<th class='text' width='17%'>$wholesale_th</th>
		<th class='text' width='17%'>$price_th</th>
		<th class='text' width='15%'>$price_th_y</th>
	</tr>
	<tr>
		<td align='center'>$plname</td>
		<td align='center'>$o_min_price</td>
		<td align='center'>$o_max_price</td>
		<td align='center'>$o_wholesale</td>
		<td align='center'>$o_price</td>
		<td align='center'>$o_rp</td>
	</tr>
</table><br><br>
<table width='550' class='bTable' cellspacing=0>
	<form action='$actionurl' name='form1' method='POST' onSubmit='return pafY();'>
	<tr>
		<th colspan='6' align='left'>$new_prices</th>
	</tr>
       	<tr>
       		<th class='text' width='17%'>$planname</th>
       		<th class='text' width='17%'>$minprice_th</th>
       		<th class='text' width='17%'>$maxprice_th</th>
       		<th class='text' width='17%'>$wholesale_th</th>
       		<th class='text' width='17%'>$price_th</th>
       		<th class='text' width='15%'>$price_th_y</th>
       	</tr>
      	<tr>
       		<td align='center'>".$plan_name."</td>
       		<td align='center'>
       			<input type='hidden' id='minpr' name='minpr' value='$minpr'>$".$minpr."</td>
       		<td align='center'>
       			<input type='hidden' id='maxpr' name='maxpr' value='$maxpr'>$".$maxpr."</td>
       		<td align='center'>$".$wholesale_mo."</td>
       		<td align='center' nowrap>$
      			<input type='text' id='new_price' name='new_price' value='$minpr' size=5 onKeyUp='calc_price();'></td>
       		<td align='center' nowrap><div align='center' id='yprice_div'>1.00</div></td>
       	</tr>
      	<tr>
       		<td colspan=6 align=center><input type='button' name='clear_button' value='$clear_form' onClick='this.form.reset();calc_price();'>
       		<input type='submit' name='submit' value='$save_plan'>
       		<input type='hidden' name='step_2_completed' value='yes'></td>";
		if (isset($control_hidden)) {
			foreach ($_POST as $key => $value) {
				if ($key != 'step2_edit') {
					echo "<input type='hidden' name='$key' value='$value'>\n";
				}
			}
			echo "<input type='hidden' name='control_type' value='3'>";
		} else {
			echo "<input type='hidden' name='prod_id' value='$prod_id'>\n";
		}
      	echo "</tr>
       	</form>
</table>
</div>
<script language='JavaScript'>
<!--
calc_price();
//-->
</script>";
$hide_rest = 'yes';
?>
